<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;

use App\Models\Attachment;
use App\Models\Admin\Info;
use Auth;
use DB;

class AdminInfoController extends AdminController
{
	/**
	 * 更新头像
	 * @return [type] [description]
	 */
    function updateAvatar(Request $request){
    	$attachment = new Attachment();
        $attachment = $attachment->uploadPic($request,80);
        
        $this->ajaxJson(0,'上传图片成功',['pic'=>$attachment->url.$attachment->fileName]);
    }

    /**
     * 更新个人信息
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    function updateInfo(Request $request){
    	$data = $request->all();
    	$admin = Auth::guard('admin')->user();
    	$info = $admin->info;
    	if(!$info){
    		$info = new Info();
    	}
    	$info->admin_id = $admin->id;
    	$info->reallyName = isset($data['reallyName']) ? $data['reallyName'] : '';
    	$info->headImg = isset($data['headImg']) ? $data['headImg'] : '';
        $info->sex = isset($data['sex']) ? $data['sex'] : 1;
        $info->age = isset($data['age']) ? $data['age'] : 18;
        $info->idNumber = isset($data['idNumber']) ? $data['idNumber'] : '';
        $info->province = isset($data['district'][0]) ? $data['district'][0] : '';
        $info->city = isset($data['district'][1]) ? $data['district'][1] : '';
        $info->area = isset($data['district'][2]) ? $data['district'][2] : '';
        $info->address =isset($data['address'][0]) ? $data['address'] : '';
        $info->save();
        $this->ajaxJson(0,'更新成功');
    }

    /**
     * 获取个人信息
     * @return [type] [description]
     */
    function getInfo(){
    	$info = Auth::guard('admin')->user()->info;
    	if(!$info){
    		$info = new Info();
    	}
    	$this->ajaxJson(0,'',$info);
    }

    /**
     * 获取当前用户权限列表
     * @return [type] [description]
     */
    public function getUserPermission(Request $request){
        $admin = Auth::guard('admin')->user();
        if($admin->isMaster){
            $permissions = array_pluck(DB::table('admin_permission')->select('name')->get()->toArray(),'name');
        }else{
            $permissions =array_pluck(DB::table('admin_permission')->select('name')->whereIn(
            'id',
                DB::table('admin_permission_role')->select('permission_id')->whereIn(
                    'role_id',
                    DB::table('admin_role_user')->select('role_id')->where(['admin_id'=>$admin->id])
                )
            )->get()->toArray(),'name');
        }
        $data = [];
        foreach ($permissions as $permission) {
            $data[str_replace('/','_', $permission)] = true;
        }
        $this->ajaxJson(0,'',$data);
    }

    /**
     * 获取个人信息 全局
     * @return [type] [description]
     */
    function getInfoCommon(){
        $user = Auth::guard('admin')->user();
        $info = $user->info;
        $roles = $user->roles;
        $roleName = $user->isMaster ? ['超级管理员'] : $roles->pluck('displayName');
        $data['loginName'] = $user['name'];
        $data['headImg'] = $info['headImg'];
        $data['roleName'] = $roleName;
        $this->ajaxJson(0,'',$data);
    }
}
